/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package oids.frontEnd.components;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import oids.frontEnd.util.ComponentStepCalculation;
import oids.frontEnd.util.Point2D;

/**
 *
 * @author CJ
 */
public class Ship extends Component implements KeyListener{

    public static int DEFAULT_THETA = 90;
    public static final int STEP_FORWARD_INTERVAL = 6;
    public static final int STEP_TURN_INTERVAL = 10;

    protected double theta;
    protected double rad;
    protected int[] threePtsX;
    protected int[] treePtsY;

    protected double currentForwardSpeed;
    protected double currentTurnSpeed;
    protected boolean firing;

    protected final static int LENGTH = 10;


    public Ship(int x, int y){
        super(x, y, Ship.LENGTH);
        this.setTheta(Ship.DEFAULT_THETA);
        this.initThreePts();
    }

    public Ship(int x, int y, double theta){
        super(x, y, Ship.LENGTH);
        this.setTheta(theta);
        this.initThreePts();
    }

    protected void initThreePts(){
        this.threePtsX = new int[3];
        this.treePtsY = new int[3];
    }

    public void setTheta(double theta){
        this.theta = theta;
    }

    public double getRad(){
        return Math.toRadians(theta);
    }

    protected void calcThreePts(){
        this.rad = Math.toRadians(this.theta);
        this.calcNosePt();
        this.calcBackLeftPt();
        this.calcBackRightPt();
    }

    protected void calcBackLeftPt(){
        double temprad = this.rad + 4 * Math.PI/3;
        this.threePtsX[1] = (int)(0.5 * Ship.LENGTH * Math.cos(temprad) + this.x);
        this.treePtsY[1] = (int)(0.5 * Ship.LENGTH * Math.sin(temprad) + this.y);
    }

    protected void calcBackRightPt(){
        double temprad = this.rad + 2 * Math.PI/3;
        this.threePtsX[2] = (int)(0.5 * Ship.LENGTH * Math.cos(temprad) + this.x);
        this.treePtsY[2] = (int)(0.5 * Ship.LENGTH * Math.sin(temprad) + this.y);
    }

    protected void calcNosePt(){
        this.threePtsX[0] = (int)(Ship.LENGTH * Math.cos(this.rad) + this.x);
        this.treePtsY[0] = (int)(Ship.LENGTH * Math.sin(this.rad) + this.y);
    }

    public void draw(Graphics g){
        calcThreePts();
        g.setColor(Color.red);
        g.fillPolygon(this.threePtsX, this.treePtsY, this.treePtsY.length);
    }

    public boolean getFiring(){
        return this.firing;
    }

    public void keyPressed(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_LEFT){
            this.currentTurnSpeed = -STEP_TURN_INTERVAL;
        }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            this.currentTurnSpeed = STEP_TURN_INTERVAL;
        }else if(e.getKeyCode() == KeyEvent.VK_UP){
            this.currentForwardSpeed = STEP_FORWARD_INTERVAL;
        }else if(e.getKeyCode() == KeyEvent.VK_DOWN){
            this.currentForwardSpeed = -STEP_FORWARD_INTERVAL;
        }else if(e.getKeyCode() == KeyEvent.VK_SPACE){
            this.firing = true;
        }
    }
    
    public void keyReleased(KeyEvent e){
        if(e.getKeyCode() == KeyEvent.VK_SPACE){
            this.firing = false;
        }else if(e.getKeyCode() == KeyEvent.VK_UP && this.currentForwardSpeed > 0){
            this.currentForwardSpeed = 0;
        }else if(e.getKeyCode() == KeyEvent.VK_DOWN && this.currentForwardSpeed < 0){
            this.currentForwardSpeed = 0;
        }else if(e.getKeyCode() == KeyEvent.VK_LEFT && this.currentTurnSpeed < 0){
            this.currentTurnSpeed = 0;
        }else if(e.getKeyCode() == KeyEvent.VK_RIGHT && this.currentTurnSpeed > 0){
            this.currentTurnSpeed = 0;
        }
    }
    
    public void keyTyped(KeyEvent e){}
    public void step(){
        stepPlacement();
        stepRotation();
    }

    protected void stepRotation(){
        this.theta += this.currentTurnSpeed;
    }

    protected void stepPlacement(){
        if(this.currentForwardSpeed != 0){
            Point2D temp = ComponentStepCalculation.getStepInterval(rad, (int)(this.currentForwardSpeed));
            this.x += temp.getX();
            this.y += temp.getY();
        }
    }

}
